package com.winhxd.b2c.order.api;


import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.context.CustomerUser;
import com.winhxd.b2c.common.context.UserContext;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.order.condition.OrderBaseConditions;
import com.winhxd.b2c.common.domain.order.condition.OrderDetailsCondition;
import com.winhxd.b2c.common.domain.order.condition.OrderQueryCondition;
import com.winhxd.b2c.common.domain.order.vo.*;
import com.winhxd.b2c.common.domain.product.condition.ProductSkuListCondition;
import com.winhxd.b2c.common.exception.BusinessException;
import com.winhxd.b2c.common.feign.product.ProductServiceClient;
import com.winhxd.b2c.common.util.JsonUtil;
import com.winhxd.b2c.order.service.OrderQueryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

/**
 * @author zsg
 * @date 2019/01/07 11:16
 */
@RestController
@Api(tags = "ApiOrderQuery")
@RequestMapping(value = "/api-order/order")
public class ApiOrderQueryController {

    @Autowired
    private OrderQueryService orderQueryService;
    @Autowired
    private ProductServiceClient productServiceClient;

    private static final Logger LOGGER = LoggerFactory.getLogger(ApiOrderQueryController.class);
    @ApiOperation(value = "C端查询订单列表", notes = "C端查询订单列表")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常")
    })
    @RequestMapping(value = "/4401/v1/getOrderList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Deprecated
    public ResponseResult<PagedList<OrderListForCustomerVO>> getOrderList(@RequestBody OrderBaseConditions condition) {
        String logTitle = "/api-order/order/4401/v1/getOrderList-C端查询订单列表";
        LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
        ResponseResult<PagedList<OrderListForCustomerVO>> result = new ResponseResult<>();
        CustomerUser customer = UserContext.getCurrentCustomerUser();
        if(null == customer.getCustomerId()){
            LOGGER.error(logTitle+"--用户为空！");
            throw new BusinessException(BusinessCode.CODE_440101);
        }
        condition.setCustomerId(customer.getCustomerId());
        PagedList<OrderListForCustomerVO> data = new PagedList<>();
        PagedList<OrderInfoVO> list = orderQueryService.findOrderList(condition);
        List<OrderInfoVO> orderInfoVOList = list.getData();
        List<OrderListForCustomerVO> orderListForCustomerVOList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(orderInfoVOList)){
            Supplier<OrderListForCustomerVO> listForCustomerVOSupplier = OrderListForCustomerVO::new;
            orderInfoVOList.forEach(orderInfoVO -> {
                OrderListForCustomerVO orderListForCustomerVO = listForCustomerVOSupplier.get();
                BeanUtils.copyProperties(orderInfoVO,orderListForCustomerVO);
                List<OrderDetailVO> orderDetailVOList = orderInfoVO.getOrderDetailVOList();
                if(!CollectionUtils.isEmpty(orderDetailVOList)){
                    List<OrderListItemForCustomerVO> orderListItemForCustomerVOList = new ArrayList<>();
                    List<Long> skuList = new ArrayList<>();
                    Supplier<OrderListItemForCustomerVO> orderListItemForCustomerVOSupplier = OrderListItemForCustomerVO::new;
                    Supplier<ProductSkuListCondition> conditionSupplier = ProductSkuListCondition::new;
                    orderDetailVOList.forEach(orderDetailVO -> {
                        //订单详情
                        OrderListItemForCustomerVO listItemForCustomerVO = orderListItemForCustomerVOSupplier.get();
                        BeanUtils.copyProperties(orderDetailVO,listItemForCustomerVO);
                        skuList.add(orderDetailVO.getSkuId());
                        orderListItemForCustomerVOList.add(listItemForCustomerVO);
                    });

                    orderListForCustomerVO.setOrderItemVoList(orderListItemForCustomerVOList);
                }
                orderListForCustomerVOList.add(orderListForCustomerVO);
            });
        }
        data.setTotalRows(list.getTotalRows());
        data.setPageNo(list.getPageNo());
        data.setPageSize(list.getPageSize());
        data.setData(orderListForCustomerVOList);
        result.setData(data);
        return result;
    }

    @ApiOperation(value = "C端查询订单列表",response = OrderInfoVO.class, notes = "C端查询订单列表")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常")
    })
    @RequestMapping(value = "/4401/v2/getOrderList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Deprecated
    public ResponseResult<PagedList<OrderInfoVO>> getOrderListV2(@RequestBody OrderQueryCondition condition) {
        String logTitle = "/api-order/order/4401/v2/getOrderList-C端查询订单列表";
        LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
        ResponseResult<PagedList<OrderInfoVO>> result = new ResponseResult<>();
        if(!StringUtils.isEmpty(condition.getCustomerSubordinateId())){
            condition.setCustomerId(condition.getCustomerSubordinateId());
        }
        if(null == condition.getCustomerId()){
            LOGGER.error(logTitle+"--用户为空！");
            throw new BusinessException(BusinessCode.CODE_440101);
        }
        result.setData(orderQueryService.selectOrderAllInfo(condition));
        LOGGER.info("{}=--结束--", logTitle);
        return result;
    }

    @ApiOperation(value = "C端查询订单列表",response = OrderInfoVO.class, notes = "C端查询订单列表")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常")
    })
    @RequestMapping(value = "/4401/v3/getOrderList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<PagedList<OrderInfoVO>> getOrderListV3(@RequestBody OrderQueryCondition condition) {
        String logTitle = "/api-order/order/4401/v3/getOrderList-C端查询订单列表";
        LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
        ResponseResult<PagedList<OrderInfoVO>> result = new ResponseResult<>();
        if(!StringUtils.isEmpty(condition.getCustomerSubordinateId())){
            condition.setCustomerId(condition.getCustomerSubordinateId());
        }
        if(null == condition.getCustomerId()){
            LOGGER.error(logTitle+"--用户为空！");
            throw new BusinessException(BusinessCode.CODE_440101);
        }
        result.setData(orderQueryService.selectOrderAllInfoV3(condition));
        LOGGER.info("{}=--结束--", logTitle);
        return result;
    }

    @ApiOperation(value = "C端订单详情查询接口",response = OrderDetailForCustomerVO.class ,notes = "C端订单详情查询接口")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常")
    })
    @RequestMapping(value = "/4402/v1/getOrderDetailByOrder", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Deprecated
    public ResponseResult<OrderDetailForCustomerVO> getOrderDetailByOrder(@RequestBody OrderDetailsCondition condition) {
        ResponseResult<OrderDetailForCustomerVO> result = new ResponseResult<>();
        String logTitle = "4402/v1/getOrderDetailByOrder--C端查询订单详情";
        if(StringUtils.isEmpty(condition.getOrderNo())){
            throw new BusinessException(BusinessCode.ORDER_NO_EMPTY);
        }
        OrderDetailForCustomerVO orderDetailForCustomerVO = orderQueryService.getOrderDetailByOrder(condition);
        result.setData(orderDetailForCustomerVO);
        return result;
    }

    @ApiOperation(value = "C端订单详情查询接口",response = OrderInfoVO.class ,notes = "C端订单详情查询接口")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"),
            @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部异常")
    })
    @RequestMapping(value = "/4402/v2/getOrderDetailByOrder", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<OrderInfoVO> getOrderDetailByOrderV2(@RequestBody OrderQueryCondition condition) {
        ResponseResult<OrderInfoVO> result = new ResponseResult<>();
        String logTitle = "4402/v2/getOrderDetailByOrder--C端查询订单详情";
        condition.setCustomerId(null);
        if(StringUtils.isEmpty(condition.getOrderNo())){
            LOGGER.error(logTitle+"--订单号为空！");
            throw new BusinessException(BusinessCode.ORDER_NO_EMPTY);
        }
        result.setData(orderQueryService.selectOrderDetailAllInfo(condition));
        LOGGER.info("{}=--结束--", logTitle);
        return result;
    }

}
